/*
 * COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * 2017年7月6日    Li.Shangzhi         Create the class
 * http://www.jimilab.com/
 */

package com.jimi.api.pay.mq;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.jimi.api.pay.vo.PayToken;
import com.jimi.commons.DatetimeUtil;
import com.jimi.samples.pay.api.IPayTokenApi;
import com.jimi.samples.pay.dto.input.PayTokenInputDto;

/**
 * @FileName PayConsumer.java
 * @Description:
 *
 * @Date 2017年7月6日 下午5:18:44
 * @author Li.Shangzhi
 * @version 1.0
 */
@Component
public class PayConsumer {

	private static final Logger logger = LoggerFactory.getLogger(PayConsumer.class);

	@Autowired
	private IPayTokenApi payTokenApi;

	@JmsListener(destination = "queue.samples.orderToken", concurrency = "5-10")
	public void receivePay(byte[] text) {
		PayToken payToken = JSONObject.parseObject(text, PayToken.class);
		logger.info("收到的支付消息：{}", payToken.toString());
		PayTokenInputDto payTokenDto = new PayTokenInputDto();
		if (StringUtils.isNotBlank(payToken.getImei())) {
			payTokenDto.setTradeNo(payToken.getImei());
			payTokenDto.setTradeType(1);
		}
		if (StringUtils.isNotBlank(payToken.getSimNo())) {
			payTokenDto.setTradeNo(payToken.getSimNo());
			payTokenDto.setTradeType(2);
		}
		payTokenDto.setUserId(payToken.getUserId());
		payTokenDto.setPayMethod(payToken.getPayMethod());
		payTokenDto.setProductDetail(payToken.getProductDetail());
		payTokenDto.setStatus(payToken.getStatus());
		payTokenDto.setAmount(payToken.getAmount());
		payTokenDto.setPayTime(DatetimeUtil.StringToDate(payToken.getPayTime()));
		payTokenApi.insert(payTokenDto);
	}
}
